package avicit.bdp.dms.dss.dto;

import avicit.bdp.common.base.BaseDTO;
import avicit.platform6.core.annotation.log.FieldRemark;
import avicit.platform6.core.annotation.log.Id;
import avicit.platform6.core.annotation.log.LogField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@ApiModel(value = "ResourceDTO", description = "bdp_dds_resources")
@Table(name = "bdp_dds_resources")
public class ResourceDTO extends BaseDTO {

    /**
     * 文件
     */
    @Transient
    public static Integer DIRECTORY_FILE = 0;

    /**
     * 目录
     */
    @Transient
    public static Integer DIRECTORY_FOLDER = 1;

    /**
     * 文件
     */
    @Transient
    public static Integer TYPE_RESOURCE = 0;

    /**
     * UDF
     */
    @Transient
    public static Integer TYPE_UDF = 1;

    /**
     * 自定义函数
     */
    @Transient
    public static Integer DTYPE_NODE = 2;

    /**
     * id
     */
    @Id
    @LogField
    @ApiModelProperty(value = "id", name = "id")
    @FieldRemark(column = "id", field = "id", name = "id")
    @javax.persistence.Id
    @Column(name = "id")
    private String id;

    @LogField
    @ApiModelProperty(value = "名称", name = "name")
    @FieldRemark(column = "name", field = "name", name = "名称")
    @Column(name = "name")
    private String name;

    @LogField
    @ApiModelProperty(value = "项目ID", name = "projectId")
    @FieldRemark(column = "project_id", field = "projectId", name = "项目ID")
    @Column(name = "project_id")
    private String projectId;

    @LogField
    @ApiModelProperty(value = "类型：0--文件；1--udf；2--自定义节点", name = "type")
    @FieldRemark(column = "type", field = "type", name = "类型")
    @Column(name = "type")
    private Integer type;

    @LogField
    @ApiModelProperty(value = "文件后缀", name = "fileSuffix")
    @FieldRemark(column = "file_suffix", field = "fileSuffix", name = "文件后缀")
    @Column(name = "file_suffix")
    private String fileSuffix;

    @LogField
    @ApiModelProperty(value = "父ID", name = "pid")
    @FieldRemark(column = "pid", field = "pid", name = "父ID")
    @Column(name = "pid")
    private String pid;

    @LogField
    @ApiModelProperty(value = "是否目录 0--文件；1--目录", name = "directory")
    @FieldRemark(column = "directory", field = "directory", name = "是否目录")
    @Column(name = "directory")
    private Integer directory;

    /**
     * 子对象集合
     */
    @Transient
    private List<ResourceDTO> children;

    @JsonIgnore
    private String createdBy;
    @JsonIgnore
    private Date creationDate;
    @JsonIgnore
    private Date lastUpdateDate;

    public ResourceDTO() {
    }

    public ResourceDTO(String id, String name, String projectId, Integer type, Integer size, String fileSuffix,
                       String remark, String pid, String fullName, Integer directory) {
        this.id = id;
        this.name = name;
        this.projectId = projectId;
        this.pid = pid;
        this.directory = directory;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Integer getDirectory() {
        return directory;
    }

    public void setDirectory(Integer directory) {
        this.directory = directory;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public List<ResourceDTO> getChildren() {
        return children;
    }

    public void setChildren(List<ResourceDTO> children) {
        this.children = children;
    }

    public String getFileSuffix() {
        return fileSuffix;
    }

    public void setFileSuffix(String fileSuffix) {
        this.fileSuffix = fileSuffix;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ResourceDTO resource = (ResourceDTO) o;
        return  directory.equals(resource.directory) &&
                id.equals(resource.id) &&
                Objects.equals(name, resource.name) &&
                Objects.equals(projectId, resource.projectId) &&
                Objects.equals(pid, resource.pid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, projectId, pid, directory);
    }

    @Override
    public String toString() {
        return "Resource{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", projectId='" + projectId + '\'' +
                ", pid='" + pid + '\'' +
                ", directory=" + directory +
                ", children=" + children +
                '}';
    }

    @Override
    public String getLogFormName() {
        if (StringUtils.isEmpty(super.logFormName)) {
            return "数据资源模块";
        } else {
            return super.logFormName;
        }
    }

    @Override
    public String getLogTitle() {
        if (StringUtils.isEmpty(super.logTitle)) {
            return "数据资源";
        } else {
            return super.logTitle;
        }
    }

}
